home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Snippets / Development Tools & Languages / DTSCPlusLibrary / Sources / MemoryClass.cp < prev    next >
Encoding:
Text File  |  1993-01-14  |  3.6 KB  |  152 lines  |  [TEXT/MPS ]

  1. /* _________________________________________________________________________________________________________ //
  2.   Copyright © 1993 Apple Computer, Inc. All rights reserved.
  3.   Macintosh Developer Technical Support.C++ Macintosh Toolbox Framework.
  4.   Programmer: Kent Sandvik
  5.   Date: 1/2/93
  6.   Revision comments are at the end of this file.
  7.   ---
  8.   TMemory is a simple object checks heap and stack values, as well as changes them.
  9.   TMemoryClass.cp contains the TMemory member function definitions. 
  10.   _________________________________________________________________________________________________________ */
  11.  
  12. #ifndef _MEMORYCLASS_
  13. #include "MemoryClass.h"
  14. #endif
  15.  
  16.  
  17. // CONSTRUCTORS AND DESTRUCTORS
  18. #pragma segment Memory
  19. TMemory::TMemory(const long minHeap,
  20.                  const long minStack)
  21. // The one and only constructor, takes min values for stack and heap if wanted, but
  22. // these are not needed.
  23. {
  24.     fMinHeap = minHeap;                            // minimum heap size
  25.     fMinStack = minStack;                        // minimum stack size
  26. }
  27.  
  28.  
  29. #pragma segment Memory
  30. TMemory::~TMemory()
  31. // Default constructor -- unused for the time being.
  32. {
  33. }
  34.  
  35.  
  36. // MAIN INTERFACE
  37. #pragma segment Memory
  38. long TMemory::GetStackSize()
  39. // Get size of the current stack.
  40. {
  41.     return ::StackSpace();                        // return size of stack
  42. }
  43.  
  44.  
  45. #pragma segment Memory
  46. Boolean TMemory::SetStackSize(const long stackValue)
  47. // Set absolute size of current stack.
  48. {
  49.     long oldValue = ::StackSpace();
  50.  
  51.     // Set the new value
  52.     SetApplLimit((Ptr)((long)GetApplLimit() - (stackValue - oldValue)));
  53.  
  54.     fError = MemError();
  55.     VASSERT(fError == noErr, ("Problems with SetApplLimit = %d", fError));
  56.  
  57.     if (fError != noErr)
  58.         return false;
  59.     else
  60.         return true;
  61. }
  62.  
  63.  
  64. #pragma segment Memory
  65. Boolean TMemory::SetStackSizeFromResources()
  66. // Get stack values from pre-defined resource (that should be part of the application).
  67. {
  68.     long newStackSize = 0;
  69.  
  70.     short num = ::CountResources(kStackResource);// find out how many we got
  71.     if (num == 0)                                // none?
  72.         goto FromResourceFalse;
  73.     else
  74.     {
  75.         for (int i = 1; i <= num; ++i)
  76.         {
  77.             Handle temp = ::GetIndResource(kStackResource, i);
  78.             fError = ResError();
  79.             VASSERT(fError == noErr, ("Problems with GetIndResource = %d", fError));
  80.             {
  81.                 const StackResource& stackValueH = **((StackValueHandle)temp);
  82.                 newStackSize += stackValueH.stackVal;
  83.             }
  84.             if (temp != NULL)
  85.                 ::ReleaseResource(temp);
  86.         }
  87.     }
  88.     this->SetStackSize(newStackSize);            // set the new stack size
  89.     return true;
  90.  
  91. FromResourceFalse:return false;
  92. }
  93.  
  94.  
  95. #pragma segment Memory
  96. Boolean TMemory::IncreaseStackSize(const long stackValue)
  97. // Increase the stack value from the current size.
  98. {
  99.     ::SetApplLimit((Ptr)((long)GetApplLimit() - stackValue));
  100.  
  101.     fError = MemError();
  102.     VASSERT(fError == noErr, ("Problems with SetApplLimit = %d", fError));
  103.  
  104.     if (fError != noErr)
  105.         return false;
  106.     else
  107.         return true;
  108. }
  109.  
  110.  
  111. #pragma segment Memory
  112. Boolean TMemory::CheckStackSize()
  113. // Check if we hit the low watermark of the stack size.
  114. {
  115.     long currentStack = ::StackSpace();
  116.  
  117.     if (currentStack < fMinStack)
  118.         return false;
  119.     else
  120.         return true;
  121. }
  122.  
  123.  
  124. #pragma segment Memory
  125. Boolean TMemory::CheckHeapSize()
  126. // Check if we hit the low watermark of the heap size.
  127. {
  128.     long currentHeap = this->GetHeapSize();
  129.  
  130.     if (currentHeap < fMinHeap)
  131.         return false;
  132.     else
  133.         return true;
  134. }
  135.  
  136.  
  137. #pragma segment Memory
  138. long TMemory::GetHeapSize()
  139. // Get size of current heap.
  140. {
  141.     return ((long)GetApplLimit() - (long)ApplicZone());
  142. }
  143.  
  144.  
  145. // _________________________________________________________________________________________________________ //
  146.  
  147. /*    Change History (most recent last):
  148.   No        Init.    Date        Comment
  149.   1            khs        1/2/93        New file
  150.   2            khs        1/3/93        Cleanup
  151. */
  152.